package com.wn.alibaba.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.wn.alibaba.cloud.sentinel.client.ProviderClient;
import com.wn.cloud.common.constant.ResponseEnum;
import com.wn.cloud.common.response.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @FileName: ConsumerController
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/4/11 - 23:10
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    private static final String URL = "http://cloud-provider-service";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProviderClient providerClient;

    @GetMapping("/{id}")
    // @SentinelResource(value = "fallback", fallback = "handleException", exceptionsToIgnore = {GlobalException.class})
    // @SentinelResource(value = "fallback", blockHandler = "blockHandle")
    @SentinelResource(value = "fallback", blockHandler = "blockHandle")
    public DataResult getData1(@PathVariable("id") long id) {
        DataResult result = providerClient.getData1(id);
        return result;
    }

    @GetMapping("data/{id}")
    public DataResult getData2(@PathVariable("id") long id) {
        DataResult result = providerClient.getData1(id);
        return result;
    }

    public DataResult blockHandle(long id, Throwable ex) {
        return DataResult.genDataResult(ResponseEnum.SENTINEL_DEFAULT_BLOCK, ex.getMessage());
    }
}
